package org.example.jedis.str;

import org.example.jedis.JedisDemo;
import org.junit.Test;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author: tianhao.luo@hand-china.com 2021/5/25  9:23
 */
public class StrControlTest extends JedisDemo {


    /**
     * 命令:set key value
     * 给键key生成值value
     */
    @Test
    public void setKeyValue(){
        String key = "topn";
        String value = "hello world";
        delExistsKeyValue(key);
        String set = jedis.set(key, value);
        LOGGER.info("添加的key为:{},value为:{},结果为:{}",key,value,set);
    }


    /**
     * 命令:get key
     * 获取键key对应的值
     */
    @Test
    public void getKey(){
        String key = "topn";
        String value = "hello world";
        delExistsKeyValue(key);
        String set = jedis.set(key, value);
        LOGGER.info("添加的key为:{},value为:{},结果为:{}",key,value,set);

        String s = jedis.get(key);
        LOGGER.info("已存在的key为:{},value为:{}",key,s);
    }

    /**
     * 命令:append key value
     * 对键key的值追加value内容到末尾,返回的结果是当前key对应的字符串被追加后的长度
     */
    @Test
    public void appendKeyValue(){
        String key = "topn";
        String value = "hello world";
        delExistsKeyValue(key);
        String set = jedis.set(key, value);
        LOGGER.info("添加的key为:{},value为:{},结果为:{}",key,value,set);

        Long append = jedis.append(key, " java");
        LOGGER.info("已存在的key为:{},append结果为:{}",key,append);
        String s = jedis.get(key);
        LOGGER.info("已存在的key为:{},value为:{}",key,s);
    }


    /**
     * 命令:strlen key
     * 获取key对应的值的长度
     */
    @Test
    public void strlenKey(){
        String key = "topn";
        String value = "hello world";
        delExistsKeyValue(key);
        String set = jedis.set(key, value);
        LOGGER.info("添加的key为:{},value为:{},结果为:{}",key,value,set);


        Long strlen = jedis.strlen(key);
        LOGGER.info("已存在的key为:{},value的长度为:{}",key,strlen);

    }

    /**
     * 命令:setnx key value
     * 当key不存在时,创建key并给值value;如果key存在,则不会替换key的值为value,且会返回0
     */
    @Test
    public void setnxKeyValue(){
        String key = "topn";
        String value = "hello world";
        delExistsKeyValue(key);
        String set = jedis.set(key, value);
        LOGGER.info("添加的key为:{},value为:{},结果为:{}",key,value,set);

        String s = jedis.get(key);
        LOGGER.info("已存在的key为:{},value为:{}",key,s);
        Long setnx = jedis.setnx(key, "1");
        LOGGER.info("第一次执行setnx的key为:{},结果为:{}",key,setnx);
        Long setnx1 = jedis.setnx(key, "1");
        LOGGER.info("第二次执行setnx的key为:{},结果为:{}",key,setnx1);
    }

    /**
     * 命令:incr key num
     * 为key对应的值value(整数)进行加num操作,返回的结果是value+num的结果
     */
    @Test
    public void incrKeyNum(){
        String key = "topn";
        String value = "1";
        delExistsKeyValue(key);
        String set = jedis.set(key,value);
        LOGGER.info("添加的key为:{},value为:{},结果为:{}",key,value,set);
        Long aLong = jedis.incrBy(key, 10L);
        LOGGER.info("添加的key为:{},incrBy结果为:{}",key,aLong);
        Long aLong1 = jedis.incr(key);
        LOGGER.info("添加的key为:{},incr结果为:{}",key,aLong1);
        String s = jedis.get(key);
        LOGGER.info("已存在的key为:{},value为:{}",key,s);
    }


    /**
     * 命令:decr key num
     * 为key对应的值value(整数)进行减num操作,返回的结果是value-num的结果
     */
    @Test
    public void decrKeyNum(){
        String key = "topn";
        String value = "10";
        delExistsKeyValue(key);
        String set = jedis.set(key,value);
        LOGGER.info("添加的key为:{},value为:{},结果为:{}",key,value,set);
        Long aLong = jedis.decrBy(key, 1L);
        LOGGER.info("添加的key为:{},decrBy结果为:{}",key,aLong);
        Long aLong1 = jedis.decr(key);
        LOGGER.info("添加的key为:{},decr结果为:{}",key,aLong1);
        String s = jedis.get(key);
        LOGGER.info("已存在的key为:{},value为:{}",key,s);
    }


    /**
     * 命令:mset key1 value1 key2 value2...
     * 批量设置键值对
     * 命令:mget key1 key2 ...
     * 批量获取key对应的值value,若不存在key,则展示nil
     */
    @Test
    public void msetKeysValuesAndMgetKeys(){
        String key1 = "k1";
        String key2 = "k2";
        String value1 = "v1";
        String value2 = "v2";
        delExistsKeyValue(key1);
        delExistsKeyValue(key2);
        String mset = jedis.mset(key1, value1, key2, value2);
        LOGGER.info("添加的key为:{},{},value为:{},{},结果为:{}",key1,key2,value1,value2,mset);
        List<String> strings = jedis.mget(key1, key2);
        for (String string : strings) {
            LOGGER.info("value为:{}",string);
        }
    }


    /**
     * 命令:msetnx key1 value1 key2 value2
     * 批量创建键值对,当创建的key已存在了,该创建命令全部失败;key都不存在时,则创建成功
     */
    @Test
    public void msetnxKeysValues(){
        String key1 = "k1";
        String key3 = "k3";
        String key4 = "k4";
        String value1 = "v1";
        String value3 = "v3";
        String value4 = "v4";
        delExistsKeyValue(key1);
        delExistsKeyValue(key3);
        delExistsKeyValue(key4);
        Long msetnx = jedis.msetnx(key4, value4, key3, value3);
        LOGGER.info("添加的key为:{},{},value为:{},{},结果为:{}",key4,key3,value4,value3,msetnx);
    }


    /**
     * 命令:getrange key startindex endindex
     * 获取键指定索引区间的字符串
     */
    @Test
    public void getrangeKeyStartindexEndindex(){
        String key = "topn";
        String value = "hello world";
        delExistsKeyValue(key);
        String set = jedis.set(key, value);
        LOGGER.info("添加的key为:{},value为:{},结果为:{}",key,value,set);
        String getrange = jedis.getrange(key, 0, 6);
        LOGGER.info("key为:{},getrange结果为:{}",key,getrange);
    }


    /**
     * 命令:setrange key offset value
     * 对key对应值,从指定偏移量开始替换为value值;偏移量从0开始计数
     */
    @Test
    public void setrangeKeyOffsetValue(){
        String key = "topn";
        String value = "hello world";
        delExistsKeyValue(key);
        String set = jedis.set(key, value);
        LOGGER.info("添加的key为:{},value为:{},结果为:{}",key,value,set);
        Long java = jedis.setrange(key, 6, "java");
        LOGGER.info("key为:{},setrange结果为:{}",key,java);
        String s = jedis.get(key);
        LOGGER.info("已存在的key为:{},value为:{}",key,s);
    }


    /**
     * 命令:setex key seconds value
     * 创建键值对时并设置过期时间;此命令和上面的expire key seconds有些类似
     * @throws Exception 休眠产生的中断异常
     */
    @Test
    public void setexKeySecondsValue() throws Exception{
        String key = "topn";
        String value = "hello world";
        delExistsKeyValue(key);
        String set = jedis.setex(key,10L, value);
        LOGGER.info("添加的key为:{},value为:{},结果为:{}",key,value,set);
        String s = jedis.get(key);
        LOGGER.info("已存在的key为:{},value为:{}",key,s);
        for (int i = 0; i < 10; i++) {
            // 自旋等待
            LOGGER.info("自旋等待中.......");
            TimeUnit.SECONDS.sleep(1);
        }
        String s1 = jedis.get(key);
        LOGGER.info("已存在的key为:{},value为:{}",key,s1);

    }

    /**
     * 命令:getset key value
     * 获取key的当前值,但是将value替换掉旧的值
     */
    @Test
    public void getsetKeyValue(){
        String key = "topn";
        String value1 = "hello world";
        String value2 = "i am chinese";
        delExistsKeyValue(key);
        String set1 = jedis.set(key, value1);
        LOGGER.info("添加的key为:{},value为:{},结果为:{}",key,value1,set1);
        String set2 = jedis.getSet(key,value2);
        LOGGER.info("getset的key为:{},value为:{},结果为:{}",key,value2,set2);
        String s = jedis.get(key);
        LOGGER.info("已存在的key为:{},value为:{}",key,s);
    }
}
